package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.dto.BreakRolesRecordDto
import cn.exrick.xboot.base.entity.BreakRolesRecord
import cn.exrick.xboot.base.mapper.BreakRolesRecordMapper
import cn.exrick.xboot.base.service.IBreakRolesRecordService
import cn.exrick.xboot.base.service.IEnterpriseEmployeeService
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.IJobService
import cn.exrick.xboot.base.vo.BreakRolesRecordDetailVo
import cn.exrick.xboot.base.vo.BreakRolesRecordListVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import cn.exrick.xboot.core.service.UserService
import cn.hutool.core.date.DateUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 违章记录接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IBreakRolesRecordServiceImpl : ServiceImpl<BreakRolesRecordMapper?, BreakRolesRecord?>(), IBreakRolesRecordService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService

    @Autowired
    private lateinit var iJobService: IJobService

    @Autowired
    private lateinit var iEnterpriseEmployeeService: IEnterpriseEmployeeService

    @Autowired
    private lateinit var userService: UserService

    override fun createOrUpdateBreakRoleRecord(breakRolesRecord: BreakRolesRecord): Result<Any?>? {

        return try {

            breakRolesRecord.breakRoleTime = DateUtil.parse(breakRolesRecord.breakRoleTimeStr, "yyyy-MM-dd HH:mm:ss")
            saveOrUpdate(breakRolesRecord)
            ResultUtil.success("操作成功")
        } catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeBreakRoleRecord(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("操作成功！")
        } catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getBreakRoleRecordList(breakRolesRecordDto: BreakRolesRecordDto, pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        val breakRolesRecordWrapper = KtQueryWrapper(BreakRolesRecord::class.java)
                .eq(BreakRolesRecord::createBy, securityUtil.currUser.id)
                .orderByDesc(BreakRolesRecord::createTime)
        if (ToolUtil.isNotEmpty(breakRolesRecordDto.enterpriseUserId)) breakRolesRecordWrapper.eq(BreakRolesRecord::enterpriseUserId,
                breakRolesRecordDto.enterpriseUserId)

       return ToolUtil.setListToNul(list(breakRolesRecordWrapper))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getBreakRolesRecordListVo(it?.id) })

            ResultUtil.data(result, "获取违章数据成功！")
        } ?: ResultUtil.error(201, "暂无数据！")
    }

    /**
     * 获取封装类
     */
    private fun getBreakRolesRecordListVo(id: String?): BreakRolesRecordListVo? {

         return getById(id)?.run {

             BreakRolesRecordListVo().apply {

                 enterpriseName = iEnterpriseUserService.getEnterpriseNameById(enterpriseUserId)
                 iEnterpriseEmployeeService.getById(employeeId)?.let {

                     jobName = iJobService.getJobName(it.jobId!!)
                     nickname = userService.get(it.userId)?.nickname
                 }
                 ToolUtil.copyProperties(this@run, this)
             }
         }
    }

    override fun getBreakRoleRecordDetail(id: String?): Result<BreakRolesRecordDetailVo>? {

        return getById(id)?.run {

            ResultUtil.data(BreakRolesRecordDetailVo().apply {

                breakRolesRecordListVo = getBreakRolesRecordListVo(id)
                nickname = userService.get(createBy)?.nickname
                breakRolePicsList = breakRolePics?.split(",")?.get(0)
                ToolUtil.copyProperties(this@run, this)
            }, "获取详情成功")
        } ?: ResultUtil.error(201, "不存在此数据！")
    }

    override fun getMyBreakRoleRecord(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(BreakRolesRecord::class.java).eq(BreakRolesRecord::employeeId, iEnterpriseEmployeeService.getEmployeeIdByUserId(securityUtil.currUser.id))
                .orderByDesc(BreakRolesRecord::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getBreakRolesRecordListVo(it?.id) }.toMutableList())

            ResultUtil.data(result, "获取我的违章记录成功")
        } ?: ResultUtil.error(201, "暂无数据！")
    }


}